<?php


define('THUMB_PATH', './resources/thumbnails/');
define('PIC_PATH',   './resources/pictures/');


/**
 * Détermine si une galerie photo existe
 */
function photo_gallery_exists($name)
{
	return file_exists(PIC_PATH.$name);
}

/**
 * Créer une galerie photo
 */
function photo_gallery_create($name)
{
	if (photo_valid_filename($name)) {
		$ok = true;
		$ok &= mkdir(THUMB_PATH.$name);
		$ok &= mkdir(PIC_PATH.$name);
		return $ok;
	}
	return false;
}

/**
 * Obtenir la liste des galeries photo
 */
function photo_gallery_list()
{
	$galleries = array();

	$dir = opendir(PIC_PATH);
	while ($file = readdir($dir)) {
		$path = PIC_PATH.$file;
		if (is_dir($path) && $file != '.' && $file != '..') {
			$galleries[] = $path;
		}
	}
	closedir($dir);
	natcasesort($galleries);
	return $galleries;
	//return glob(PIC_PATH.'*', GLOB_ONLYDIR); //con de glob
}

/**
 * Renommer une galerie photo
 */
function photo_gallery_rename($old_name, $new_name)
{
	if (photo_gallery_exists($old_name)
		&& photo_valid_filename($new_name)) {
		$ok = true;
		$ok &= rename(PIC_PATH.$old_name, PIC_PATH.$new_name);
		$ok &= rename(THUMB_PATH.$old_name, THUMB_PATH.$new_name);
		return $ok;
	}
	return false;
}

/**
 * Supprimer une galerie et toutes les photos qu'elle contient
 */
function photo_gallery_delete($gallery)
{
	if (photo_gallery_exists($gallery)) {
		$ok = true;
		// suppression des photos
		$pics = photo_list($gallery);
		foreach ($pics as $pic) {
			$ok &= photo_delete($gallery, basename($pic));
		}
		// suppression des répertoires
		if ($ok) {
			$ok &= rmdir(THUMB_PATH.$gallery);
			$ok &= rmdir(PIC_PATH.$gallery);
		}
		return $ok;
	}
	return false;
}


/**
 * Obtenir la liste des photos d'une galerie
 */
function photo_list($gallery)
{
	$photos = array();

	$dir = opendir(THUMB_PATH.$gallery);
	while ($file = readdir($dir)) {
		$path = THUMB_PATH.$gallery.'/'.$file;
		if (is_file($path)) {
			$photos[] = $path;
		}
	}
	closedir($dir);
	return $photos;
	//return glob(THUMB_PATH.$gallery.'/*.jpg');
}

/**
 * Obtenir l'url d'une photo dans une galerie
 */
function photo_get_link($gallery, $picture)
{
	return PIC_PATH.$gallery.'/'.$picture;
}

/**
 * Ajouter une photo dans une galerie et créer la miniature
 * @param gallery: nom de la galerie
 * @param file_pic: image uploadée
 * @param resize: si true, redimensionnement de l'image à PIC_MAX_WIDTH
 */
function photo_add($gallery, $file_pic, $resize)
{
	$thumb_quality = intval(PIC_THUMB_QUALITY);
	$pic_quality = intval(PIC_QUALITY);

	if ($file_pic['error'] <= 0)
	{
		$extension = strtolower(substr(strrchr($file_pic['name'], '.'), 1));
		if ($extension != 'jpg' && $extension != 'jpeg') {
			return false;
		}

		$pic_name = strtolower(clean_str($file_pic['name']));
		$picture = imagecreatefromjpeg($file_pic['tmp_name']);

		// calcul dimensions du thumbnail
		$pic_size = getimagesize($file_pic['tmp_name']);
		$width = $pic_size[0];
		$height = $pic_size[1];
		if ($width > $height) {
			$src_x = ($width - $height) / 2;
			$src_y = 0;
			$smaller_size = $height;
		}
		else {
			$src_x = 0;
			$src_y = ($height - $width) / 2;
			$smaller_size = $width;
		}

		// création du thumbnail carré
		$thumb = imagecreatetruecolor(PIC_THUMB_SIZE, PIC_THUMB_SIZE);
		if (!$thumb) {
			return false;
		}
		// redimensionnement
		imagecopyresampled($thumb, $picture, 0, 0, $src_x, $src_y,
			PIC_THUMB_SIZE, PIC_THUMB_SIZE, $smaller_size, $smaller_size);
		// sauvegarde du thumbnail
		imagejpeg($thumb, THUMB_PATH.$gallery.'/'.$pic_name, PIC_THUMB_QUALITY);

		if ($resize && $width > PIC_MAX_WIDTH) {
			// redimensionnement de l'image d'origine

			$new_height = $height * PIC_MAX_WIDTH / $width;
			$new_width = PIC_MAX_WIDTH;

			$new_pic = imagecreatetruecolor($new_width, $new_height);
			if (!$new_pic) {
				return false;
			}
			imagecopyresampled($new_pic, $picture, 0, 0, 0, 0,
				$new_width, $new_height, $width, $height);
			imagejpeg($new_pic, photo_get_link($gallery, $pic_name), PIC_QUALITY);
		}
		else {
			// sauvegarde de l'image d'origine
			if (!move_uploaded_file($file_pic['tmp_name'],
					photo_get_link($gallery, $pic_name))) {
				return false;
			}
		}
		imagedestroy($picture);
		return true;
	}
	return false;
}

/**
 * @return true si le nom est valide
 */
function photo_valid_filename($test_name)
{
	return strpos($test_name, '/') == false
		&& strpos($test_name, '-') == false
		&& strpos($test_name, '#') == false;
}

/**
 * Renommer une photo
 */
function photo_rename($gallery, $current_name, $new_name)
{
	if (!photo_exists($gallery, $new_name) && photo_valid_filename($new_name)) {
		$ok = true;
		// renommer la photo originale
		$ok &= rename(PIC_PATH.$gallery.'/'.$current_name, PIC_PATH.$gallery.'/'.$new_name);
		// renommer la miniature
		$ok &= rename(THUMB_PATH.$gallery.'/'.$current_name, THUMB_PATH.$gallery.'/'.$new_name);
		return $ok;
	}
	return false;
}

/**
 * Supprimer une photo
 */
function photo_delete($gallery, $name)
{
	$ok = true;
	$ok &= unlink(THUMB_PATH.$gallery.'/'.$name);
	$ok &= unlink(photo_get_link($gallery, $name));
	return $ok;
}

/**
 * @return true si la photo existe déjà
 */
function photo_exists($gallery, $name)
{
	return file_exists(photo_get_link($gallery, $name));
}
